	 
********************************************************************************
//Random exams
cd $pathdata_processed
use terminales, clear

//sample selection
keep if (inlist(nus2000, 401101, 441106) & year <= 2007) | (inlist(nus2000, 401111, 401112) & year >= 2008) //general or economic and business program
drop if N_tilg>1 //enrolled in one course
keep if status == 2 & heldel == 1 //student status & full time
drop if nb_komp>1 //not in technical track before
drop if missing(skole_) | missing(skolekom) //information on school
drop if inlist(fylke, 21, 25) //counties with almost no students (svalbard)
drop status heldel komp nb_komp N_*

//courses and grades 
//1) keep courses taken for the first time
cd $pathdata_education
merge 1:m npid using tab_karakter_vgskole_2003-2018.dta, keep(1 3) nogen //grades and courses in high school

replace fagkode = "KRO1001" if fagkode =="KR¿1001"
replace fagkode = "KRO1002" if fagkode =="KR¿1002"
replace fagkode = "KRO1003" if fagkode =="KR¿1003"
replace fagkode = "VG4001" if fagkode =="VG400¿"

keep if real(substr(skolear, 1, 4)) <= year //years before and including VKII
merge m:1 fagkode using fagkatalog_2020, keep(1 3) nogen

drop termin* kar_annen vide

duplicates drop 

replace fagstatus = "Z" if fagstatus == "A"
gen same_school = -(skole_ == skule)

foreach var in stp skr mun {
	replace `var' = "1" if inlist(`var', "0", "9", "IV", "IM", "ID", "IB", "IG", "X", "Z")
}

gen real_stp = real(stp)
replace real_stp = . if !inlist(real_stp, 1, 2, 3, 4, 5, 6)

gen real_skr = real(skr)
replace real_skr = . if !inlist(real_skr, 1, 2, 3, 4, 5, 6)

gen real_mun = real(mun)
replace real_mun = . if !inlist(real_mun, 1, 2, 3, 4, 5, 6)

duplicates drop

gen aartimer_ = -aartimer
bysort npid fagkode (skolear fagstatus aartimer_ same_school real_stp real_skr real_mun stp skr mun skule): keep if _n == 1 //foreach course, we keep the first time students take it
drop aartimer_ same_school

keep if year == real(substr(skolear, 1, 4)) //VKII year

//sample selection: we want to exclude courses taken as a private student
drop if missing(skule) 
drop if fagstatus != "E" & year>=2007
drop if missing(real_stp)

gen exam = !missing(skr)
gen oral = !missing(mun)

bysort npid: egen nb_kurs = count(fagkode) 
bysort npid: egen nb_exam = sum(exam)
bysort npid: egen nb_oral = sum(oral)

//define type of exams for each courses
order npid year nus2000 skole_ fylke fagkode fagtype fagnavn nb_kurs nb_exam nb_oral exam oral stp skr mun real_stp real_skr real_mun 
keep npid year nus2000 skole_ fylke fagkode fagtype fagnavn nb_kurs nb_exam nb_oral exam oral stp skr mun real_stp real_skr real_mun 

egen fagkode_num = group(fagkode)

cd $pathdata_processed
save sample_tirage, replace

